package com.ma.process.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ma.model.process.ProcessTemplate;
import com.ma.model.process.ProcessType;
import com.ma.process.mapper.OaProcessTypeMapper;
import com.ma.process.service.OaProcessTemplateService;
import com.ma.process.service.OaProcessTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OaProcessTypeServiceImpl extends ServiceImpl<OaProcessTypeMapper, ProcessType> implements OaProcessTypeService {
    @Autowired
    private OaProcessTemplateService processTemplateService;

    @Override
    public Page<ProcessType> index(Long page, Long limit) {
        Page<ProcessType> p = new Page<>(page,limit);
        return this.page(p);

    }

    //查询所有的审批类型和审批模板
    @Override
    public List<ProcessType> findProcessType() {
        //查询所有审批分类
        List<ProcessType> processTypes = baseMapper.selectList(null);
        //遍历返回的所有审批类型
        for (ProcessType processType : processTypes){
            if(processType == null){
                continue;
            }
            //得到每个审批分类，根据审批分类的id查询审批模板，封装到每个审批类型中去
            Long typeId = processType.getId();
            List<ProcessTemplate> templateList = processTemplateService.list(new LambdaQueryWrapper<ProcessTemplate>()
                    .eq(ProcessTemplate::getProcessTypeId, typeId));
            processType.setProcessTemplateList(templateList);
        }
        return processTypes;
    }
}
